# Get build the User ID list

USER_DB_DIR = "./user_db/"
HTTP_PAYLOAD_DIR = "./http_payload/"
GLOBAL_MAP = {}

module BuildUserDB
  def self.get_all_files
    Common.get_all_files(HTTP_PAYLOAD_DIR)    
  end

  def self.handle_all_files(all_files)
    all_files.each do |file_name|
      puts "--> " + file_name
      handle_each_file file_name
      puts "<-- " + file_name
    end
  end

  def self.handle_each_file(file_name)
    puts "now handling : " + file_name
    file = File.open HTTP_PAYLOAD_DIR + file_name, "r"

    begin
      while line = file.readline
        if line.include? "facebook"
          fields = line.split("|")
          client_ip = fields[5]
          db_key = (client_ip.split ".")[-1]

          # select the db to store the lines
          db = get_target_db db_key

          if db[client_ip]
            db[client_ip] = db[client_ip] + "}${" + line
          else
            db[client_ip] = line
          end

        end
      end
    rescue Exception => e
      puts e.to_s
    end
  end

  def self.get_target_db(key)
    unless GLOBAL_MAP.include_key? key
      db = DBM.open(USER_DB_DIR + key)
      GLOBAL_MAP[key] = db
    end

    GLOBAL_MAP[key]
  end

  def self.close_all_db
    GLOBAL_MAP.each_value do |db|
      db.close
    end
  end

  def self.run
    puts ">> start to get facebook IDs ..."

    begin
      all_files = BuildUserDB.get_all_files      
      all_facebook_id = BuildUserDB.handle_all_files all_files
      puts ">> end to build user dbs"
    rescue Exception => e
      puts ">> EEROR:"
      puts e.to_s
    end
  end

end    